
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Ripyl VCD tutorial &mdash; Ripyl 1.2 documentation</title>
    
    <link rel="stylesheet" href="../_static/nature.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../_static/ripyl.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="top" title="Ripyl 1.2 documentation" href="../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../index.html">Ripyl 1.2 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="ripyl-vcd-tutorial">
<h1>Ripyl VCD tutorial<a class="headerlink" href="#ripyl-vcd-tutorial" title="Permalink to this headline">¶</a></h1>
<p>This tutorial demonstrates how to generate Value Change Dump (VCD) files from a protocol simulation.</p>
<p>You can use the Ripyl simulation facilities to generate VCD test vectors with the <a class="reference internal" href="../apidoc/ripyl.io.html#module-ripyl.io.vcd" title="ripyl.io.vcd"><tt class="xref py py-mod docutils literal"><span class="pre">ripyl.io.vcd</span></tt></a> module. These files can be used to generate stimulus in applications such as pattern generators and HDL simulations.</p>
<p>The procedure for creating a VCD file is to first define a sequence of VCD channels, each containing the necessary data and a name. A VCDInfo object is then created which can be used to write a VCD file.</p>
<p>The VCD format uses integer time stamps with a resolution established by a timescale parameter. All edge transitions that occur within one timescale unit will be merged to the same time stamp. If the timescale is too large, entire edge transitions may be omitted if the state does not change between the start and end of a timescale unit. The timescale can either be explicitly set by passing a float to the VCDInfo constructor or it can be determined automatically with symbol rate analysis on a specified channel. Valid timescales must be powers of 10. In both cases the raw timescale parameter is coerced to a valid power of 10 less than or equal to the raw value (e.g. 3.0e-8 is coerced to 1.0e-8).</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">ripyl.io.vcd</span> <span class="kn">as</span> <span class="nn">vcd</span>
<span class="kn">import</span> <span class="nn">ripyl.protocol.spi</span> <span class="kn">as</span> <span class="nn">spi</span>
<span class="kn">import</span> <span class="nn">random</span>

<span class="c"># Simulate some SPI communication</span>
<span class="n">data</span> <span class="o">=</span> <span class="p">[</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">255</span><span class="p">)</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="mi">10</span><span class="p">)]</span>
<span class="n">clk</span><span class="p">,</span> <span class="n">mosi</span><span class="p">,</span> <span class="n">cs</span> <span class="o">=</span> <span class="n">spi</span><span class="o">.</span><span class="n">spi_synth</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mf">100e3</span><span class="p">)</span>

<span class="c"># Define the channels included in the VCD</span>
<span class="n">channels</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">vcd</span><span class="o">.</span><span class="n">VCDChannel</span><span class="p">(</span><span class="s">&#39;clk&#39;</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">clk</span><span class="p">)),</span>
    <span class="n">vcd</span><span class="o">.</span><span class="n">VCDChannel</span><span class="p">(</span><span class="s">&#39;mosi&#39;</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">mosi</span><span class="p">)),</span>
    <span class="n">vcd</span><span class="o">.</span><span class="n">VCDChannel</span><span class="p">(</span><span class="s">&#39;cs&#39;</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">cs</span><span class="p">))</span>
<span class="p">]</span>

<span class="c"># Explicitly specify the timescale ...</span>
<span class="n">spi_vcd</span> <span class="o">=</span> <span class="n">vcd</span><span class="o">.</span><span class="n">VCDInfo</span><span class="p">(</span><span class="n">channels</span><span class="p">,</span> <span class="n">timescale</span><span class="o">=</span><span class="mf">1.0e-6</span><span class="p">,</span> <span class="n">comment</span><span class="o">=</span><span class="s">&#39;SPI simulation&#39;</span><span class="p">)</span>

<span class="c"># ... or, alternatively, use the symbol rate of &#39;clk&#39; to set the timescale</span>
<span class="n">spi_vcd</span> <span class="o">=</span> <span class="n">vcd</span><span class="o">.</span><span class="n">VCDInfo</span><span class="p">(</span><span class="n">channels</span><span class="p">,</span> <span class="n">timescale</span><span class="o">=</span><span class="s">&#39;clk&#39;</span><span class="p">,</span> <span class="n">comment</span><span class="o">=</span><span class="s">&#39;SPI simulation&#39;</span><span class="p">)</span>

<span class="k">print</span> <span class="s">&#39;Timescale is:&#39;</span><span class="p">,</span> <span class="n">spi_vcd</span><span class="o">.</span><span class="n">timescale_si</span>

<span class="c"># Write the VCD file</span>
<span class="n">spi_vcd</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;spi.vcd&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>This produces a VCD file <cite>spi.vcd</cite> that can be opened in a waveform viewer like GTKWave:</p>
<a class="reference internal image-reference" href="../_images/vcd_gtkwave.png"><img alt="../_images/vcd_gtkwave.png" src="../_images/vcd_gtkwave.png" style="width: 692.0px; height: 196.0px;" /></a>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/Ripyl logo 100px.png" alt="Logo"/>
            </a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../_sources/rst/tut_vcd.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../index.html">Ripyl 1.2 documentation</a> &raquo;</li> 
      </ul>
    </div>

    <div class="footer">
        &copy; Copyright 2013, Kevin Thibedeau.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>

<script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-43149311-1', 'google.com');
  ga('send', 'pageview');

</script>

  </body>
</html>